import { observable, action } from "mobx";
import * as api from "../api/api";
import request from "../utils/request";
import { LoadHoc, loading } from '../hoc/loadHoc';
import {CommPageInputCommTypeEnum} from "../api/api";

export default class UserStore extends LoadHoc {

  /**
   * 获取验证码
   */
  @observable smsCode:string = ''
  @loading @action async getSmsCode(params: {
    form: api.SendCodeForm,
    callback?: (smsCode: string) => void;
  }){
    const { form, callback } = params;
    const smsCode = await request(api.FrontUserApiFetchParamCreactor.sendCodeAsync({
      form
    }));
    callback&&callback(smsCode)
  }

  /**
   * 用户登录
   */
  @loading @action async userLogin(params: {
    form: api.FrontUserInput,
    callback: (res) => void
  }){
    const { form, callback } = params
      const userLoginRes = await request(api.FrontUserApiFetchParamCreactor.loginAsync({
      form
    }));
    callback&&callback(userLoginRes)
  }


  /**
   * 用户登出
   */
  @loading @action async logout(params: {
    callback: (res) => void
  }){
    const { callback } = params
    const res = await request(api.FrontUserApiFetchParamCreactor.appuserLogoutAsync({}));
    callback&&callback(res)
  }

  /**
   * 获取用户地址信息列表
   */
  @loading @action async getAddressList(params: {
    callback: (res: api.AddressInput) => void
  }){
    const { callback } = params
    const res = await request(api.FrontUserApiFetchParamCreactor.getAddressListAsync());
    callback&&callback(res)
  }

  /**
   * 保存用户地址列表
   */
  @loading @action async saveAddress(params: {
    input: api.AddressInput,
    callback: (res: api.AddressInput) => void
  }){
    const { input, callback } = params
    const res = await request(api.FrontUserApiFetchParamCreactor.saveAddressSaync({
      input
    }));
    callback&&callback(res)
  }

  /**
   * 删除用户地址列表
   */
  @loading @action async removeAddress(params: {
    id: string,
    callback: (res) => void
  }){
    const { id, callback } = params
    const res = await request(api.FrontUserApiFetchParamCreactor.removeAddressAsync({
      id
    }));
    callback&&callback(res)
  }

  /**
   * 获取银行卡列表
   */
  @loading @action async getUserCardsAsync(params: {
    callback: (res: api.UserCardOutput) => void
  }){
    const { callback } = params
    const res = await request(api.FrontUserApiFetchParamCreactor.getUserCardsAsync());
    callback&&callback(res)
  }

  /**
   * 添加银行卡
   */
  @loading @action async saveUserCardAsync(params: {
    form: api.UserCardInput,
    callback: (res: api.UserCardOutput) => void
  }){
    const { form, callback } = params
    const res = await request(api.FrontUserApiFetchParamCreactor.saveUserCardAsync({
      form
    }));
    callback&&callback(res)
  }

  /**
   * 删除银行卡
   */
  @loading @action async removeUserCardAsync(params: {
    cardId: string,
    callback: (res: api.UserCardOutput) => void
  }){
    const { cardId, callback } = params
    const res = await request(api.FrontUserApiFetchParamCreactor.removeUserCardAsync({
      cardId
    }));
    callback&&callback(res)
  }

  /**
   * 获取签到记录
   */
  @loading @action async getUserSignRecordAsync(params: {
    callback: (res: api.SignOutput) => void
  }){
    const { callback } = params
    const res = await request(api.SignApiFetchParamCreactor.getUserSignRecordAsync());
    callback&&callback(res)
  }

  /**
   * 签到
   */
  @loading @action async signAsync(params: {
    callback: (res: api.SignOutput) => void
  }){
    const { callback } = params
    const res = await request(api.SignApiFetchParamCreactor.signAsync());
    callback&&callback(res)
  }

  /**
   * 获取用户信息
   */
  @loading @action async getUserInfoAsync(params: {
    callback: (res: api.UserOutput) => void
  }){
    const { callback } = params
    const res = await request(api.FrontUserApiFetchParamCreactor.getUserInfoAsync());
    callback&&callback(res)
  }

  /**
   * 获取收益信息
   */
  @loading @action async userProfitAsync(params: {
    callback: (res: api.UserProfitOutput) => void
  }){
    const { callback } = params
    const res = await request(api.FrontUserApiFetchParamCreactor.userProfitAsync());
    callback&&callback(res)
  }

  /**
   * 获取收益记录
   */
  @loading @action async incomeRecord(params: {
      commType: api.CommPageInputCommTypeEnum,
      pageIndex: number,
      pageSize: number,
    callback: (res: api.CommOutput) => void
  }){
    const { commType, pageIndex, pageSize, callback } = params
    const res = await request(api.FrontUserApiFetchParamCreactor.incomeRecord({
      form: {
        commType,
        skipCount:  (pageIndex-1)*pageSize,
        maxResultCount:pageSize
      }
    }));
    callback&&callback(res)
  }

  /**
   * 绑定/修改手机号
   */
  @loading @action async setUserPhoneAsync(params: {
    form: api.VerifyCodeForm,
    callback: (res: api.UserProfitOutput) => void
  }){
    const { form, callback } = params
    const res = await request(api.FrontUserApiFetchParamCreactor.setUserPhoneAsync({
        form
      }));
    callback&&callback(res)
  }

  /**
   * 用户体现
   */
  @loading @action async saveUserCash(params: {
    form: api.CashInput,
    callback: (res: api.UserProfitOutput) => void
  }){
    const { form, callback } = params
    const res = await request(api.FrontUserApiFetchParamCreactor.saveUserCash({
      form
    }));
    callback&&callback(res)
  }

  /**
   * 用户体现记录
   */
  @loading @action async userCashRecord(params: {
    pageIndex: number,
    pageSize: number,
    callback: (res: api.CashOutput) => void
  }){
    const { pageIndex, pageSize, callback } = params
    const res = await request(api.FrontUserApiFetchParamCreactor.userCashRecord({
      form: {
        skipCount:  (pageIndex-1)*pageSize,
        maxResultCount:pageSize
      }
    }));
    callback&&callback(res)
  }

  /**
   * 我的团队
   */
  @loading @action async userTeamPage(params: {
    gradeId: string,
    pageIndex: number,
    pageSize: number,
    callback: (res: api.UserProfitOutput) => void
  }){
    const { gradeId, pageIndex, pageSize, callback } = params
    const res = await request(api.FrontUserApiFetchParamCreactor.userTeamPage({
      form: {
        gradeId,
        skipCount:  (pageIndex-1)*pageSize,
        maxResultCount:pageSize
      }
    }));
    callback&&callback(res)
  }

  /**
   * 我的团队-团队成员
   */
  @loading @action async userTeamById(params: {
    id: string,
    callback: (res: api.UserTeamOutput) => void
  }){
    const { id, callback } = params
    const res = await request(api.FrontUserApiFetchParamCreactor.userTeamById({
      form: { id }
    }));
    callback&&callback(res)
  }


  /**
   * 绑定上级用户ID
   */
  @loading @action async bindingSourceUser(params: {
    phoneNumber: string,
    callback: (res: api.UserTeamOutput) => void
  }){
    const { phoneNumber, callback } = params
    const res = await request(api.FrontUserApiFetchParamCreactor.bindingSourceUser({
      phoneNumber
    }));
    callback&&callback(res)
  }

}
